//
//  UserInfoItem.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/6.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "UserInfoItem.h"
#import <YYKit/NSData+YYAdd.h>
#import <YYKit/YYKit.h>

@implementation UserInfoItem

+ (NSString *)primaryKey {
    return @"userId";
}

+(NSDictionary *)defaultPropertyValues{
    return @{   @"userId": @"",
                @"icon": @"",
                @"lastName": @"",
                @"FirstName":@"",
                @"signature": @"",
                @"gender": @"0",
                @"email": @"",
                @"weight": @"50",
                @"height": @"1.70",
                @"birthYear": @"1990",
                @"address":@"",
                @"handBallType": @"0",
                @"racquetType":@(RacquetLight),
                @"trainingTarget":@"",
                @"passWord":@"",
                @"countryID":@"",
                @"provinceID":@"",
                @"cityID":@"",
                @"phoneNum": @"",
                @"createTime": @"2017-11-15",
                @"updateTime": @"2017-11-15",
                @"lastLoginVersion":@"",
                @"loginType":@(LoginTypeEmail),
                };
}

+ (UserInfoItem *)userInfoWithPrivateKey:(NSString *)key
{
    RLMRealm *realm = [RLMRealm defaultRealm];
    UserInfoItem *userInfo = [UserInfoItem objectForPrimaryKey:key];
    if (userInfo == nil) {
        userInfo = [[UserInfoItem alloc] init];
        userInfo.userId = key;
        [realm beginWriteTransaction];
        [realm addObject:userInfo];
        [realm commitWriteTransaction];
    }
    return userInfo;
}
#pragma mark -  数据转换
// 将数据替换成userInfo的值,并存储
- (void)copyValuesFrom:(UserInfoItem *)userInfo {
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
         [userInfo copyValuesTo:self];
    }];
}

- (void)copyValuesTo:(UserInfoItem *)infoParams {
    infoParams.icon = self.icon;
    infoParams.lastName = self.lastName;
    infoParams.FirstName = self.FirstName;
    infoParams.signature = self.signature;
    infoParams.gender = self.gender;
    infoParams.email = self.email;
    infoParams.passWord = self.passWord;
    infoParams.weight = self.weight;
    infoParams.height = self.height;
    infoParams.birthYear = self.birthYear;
    infoParams.address = self.address;
    infoParams.handBallType = self.handBallType;
    infoParams.racquetType = self.racquetType;
    infoParams.trainingTarget = self.trainingTarget;
    infoParams.countryID = self.countryID;
    infoParams.provinceID = self.provinceID;
    infoParams.cityID = self.cityID;
    infoParams.createTime = self.createTime;
    infoParams.cityID = self.cityID;
    infoParams.phoneNum = self.phoneNum;
    infoParams.updateTime = self.updateTime;
    infoParams.lastLoginVersion = self.lastLoginVersion;
    infoParams.loginType = self.loginType;
}

- (void)parseUserInfoDictionary:(NSDictionary *)dict {
    //    self.userId = dict[@"UserID"];
    self.address = dict[@"Address"];
    self.birthYear = dict[@"Age"];
    self.createTime = dict[@"CreateTime"];
    self.gender = (Gender)[dict[@"Gender"] integerValue];
    self.handBallType = [dict[@"Hand"] isEqualToString:@"1"] ? HandDirectionTypeBackward:HandDirectionTypeForeward;
    self.address = dict[@"Address"];
    self.height = dict[@"Height"];
    self.icon = dict[@"Icon"];
    self.lastName = dict[@"LastName"];
    self.FirstName = dict[@"FirstName"];
    self.email = dict[@"Email"];
    self.racquetType = (RacquetType)[dict[@"Racquet"] integerValue];
    NSArray *joinClubList = dict[@"JoinClubList"];
    NSArray *adminClubList = dict[@"AdminClubList"];

    for (NSString *clubID in joinClubList) {
        JCClubIDString *clubIDModel = [JCClubIDString objectForPrimaryKey:clubID];
        if (clubIDModel == nil) {
            clubIDModel = [JCClubIDString new];
            clubIDModel.clubID = clubID;
        }
        if ([self.joinClubs objectsWhere:@"clubID == %@",clubID].count == 0) {
            [self.joinClubs addObject:clubIDModel];
        }
        
    }
    for (NSString *clubID in adminClubList) {
        JCClubIDString *clubIDModel = [JCClubIDString objectForPrimaryKey:clubID];
        if (clubIDModel == nil) {
            clubIDModel = [JCClubIDString new];
            clubIDModel.clubID = clubID;
        }
        if ([self.adminClubs objectsWhere:@"clubID == %@",clubID].count == 0) {
            [self.adminClubs addObject:clubIDModel];
        }
    }
   // NSArray *trainArr = [dict[@"Training"] componentsSeparatedByString:@"-"];
    self.trainingTarget = dict[@"Training"];
    /*
    if (trainArr.count == 2) {
       TrainingTimeSectionType sectionType = [trainArr.firstObject integerValue];
       self.trainingTarget = [NSString stringWithFormat:@"%@hour %@",trainArr.lastObject,[SquashEnum stringWithTimeSectionType:sectionType]];
    }
    */
    
    self.updateTime = dict[@"UpdateTime"];
    self.loginType = (LoginType)[dict[@"Type"] integerValue];
    self.weight = dict[@"Weight"];
}

#pragma  mark - 数据计算处理
- (NSInteger)getAge {
   return [NSDate date].year - [NSDate dateWithString:self.birthYear format:@"yyyy"].year;
}

- (NSString *)nickName {
    return [NSString stringWithFormat:@"%@ %@",self.FirstName,self.lastName];
}

- (NSInteger)ftFormHeight {
    NSInteger ft = 0;
    CGFloat inch = 0;
    inch = [Common cmToInch:[self.height floatValue] * 100.0];
    ft = (NSInteger)(inch / 12); //英尺
    inch = (inch - ft * 12);
    return ft;
}
- (CGFloat)inchFormHeight {
    NSInteger ft = 0;
    CGFloat inch = 0;
    inch = [Common cmToInch:[self.height floatValue] * 100.0];
    ft = (NSInteger)(inch / 12); //英尺
    inch = (inch - ft * 12);
    return inch;
}

+ (NSString *)ft:(NSInteger)ft inch:(NSInteger)inch {
    CGFloat height = [Common inchToCm:(ft * 12 + inch)] * 0.01;
    return [NSString stringWithFormat:@"%.2f",height];
}

- (BOOL)isAdminForClubID:(NSString *)clubID{
    BOOL admin = NO;
    for (JCClubIDString *clubIDModel in self.adminClubs) {
        if ([clubID isEqualToString:clubIDModel.clubID]) {
            admin = YES;
        }
    }
    return admin;
}

/*判断俱乐部类型*/
- (ClubUserType)clubUserType:(NSString *)clubID{
    for (JCClubIDString *clubIDModel in self.adminClubs) {
        if ([clubID isEqualToString:clubIDModel.clubID]) {
            return ClubUserTypeAdmin;
        }
    }
    for (JCClubIDString *clubIDModel in self.joinClubs) {
        if ([clubID isEqualToString:clubIDModel.clubID]) {
            return ClubUserTypeGeneral;
        }
    }
    return ClubUserTypeVisitor;
}

- (ClubUserType)clubUserType{
    if (self.adminClubs.count > 0) {
        return ClubUserTypeAdmin;
    }else if (self.joinClubs.count > 0){
        return ClubUserTypeGeneral;
    }else{
        return ClubUserTypeVisitor;
    }
}

@end




@implementation JCClubIDString

+ (NSString *)primaryKey {
    return @"clubID";
}

+ (JCClubIDString *)clubIDStringWithPrivateKey:(NSString *)key{
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    JCClubIDString *clubID = [JCClubIDString objectForPrimaryKey:key];
    if (clubID == nil) {
        clubID = [[JCClubIDString alloc] init];
        clubID.clubID = key;
        [realm beginWriteTransaction];
        [realm addObject:clubID];
        [realm commitWriteTransaction];
    }
    return clubID;
}

@end
